package com.sxjh.controller.ny;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sxjh.annotation.UserLog;
import com.sxjh.common.*;
import com.sxjh.entity.GoodsLocation;
import com.sxjh.entity.PlannedOrdersEntity;
import com.sxjh.entity.WorkshopEntity;
import com.sxjh.entity.param.WorkshopAddParam;
import com.sxjh.entity.param.WorkshopList2Param;
import com.sxjh.entity.param.WorkshopListParam;
import com.sxjh.enums.DelFlagEnum;
import com.sxjh.service.GoodsLocationService;
import com.sxjh.service.PlannedOrdersService;
import com.sxjh.service.WorkshopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 内部倒运--车间管理 前端控制器
 * </p>
 *
 * @author guanxy
 * @since 2023-05-29
 */
@Api(tags = "车间管理")
@RestController
@RequestMapping("/workshop")
public class WorkshopController extends BaseController {


    @Autowired
    private WorkshopService workshopService;

    @Autowired
    private GoodsLocationService goodsLocationService;

    @Autowired
    private PlannedOrdersService plannedOrdersService;

    @ApiOperation("分页列表")
    @GetMapping(value = "/workshop.list")
    public Result<ZPage<WorkshopList2Param>> list(WorkshopListParam param) {
        IPage iPage = new Page(param.getCurrent(), param.getSize());
        if (param.getSize() == -1) {
            iPage.setSize(Constant.maxSize);
        }
        IPage<WorkshopListParam> aPage = workshopService.queryPage(iPage, param);
        ZPage<WorkshopList2Param> zPage = copyPage(aPage, WorkshopList2Param::new);
        return Result.ok(zPage);
    }

    @ApiOperation("查询")
    @GetMapping(value = "/{id}")
    public Result<WorkshopEntity> getById(@PathVariable("id") String id) {
        WorkshopEntity workshopEntity = workshopService.getById(id);
        return Result.ok(workshopEntity);
    }

    @ApiOperation("新增")
    @PostMapping(value = "/add")
    @UserLog(desc = "内部倒运-新增车间", roleCode = "nbdy.workshop.add")
    public Result<Object> create(@RequestBody WorkshopAddParam params) throws ResultException {
        if (StrUtil.isNotBlank(params.getName())) {
            LambdaQueryWrapper<WorkshopEntity> query1 = new LambdaQueryWrapper<>();
            query1.eq(WorkshopEntity::getName, params.getName());
            query1.eq(WorkshopEntity::getDelFlag, DelFlagEnum.NO.getType());
            List<WorkshopEntity> list = workshopService.list(query1);
            if (CollUtil.isNotEmpty(list)) {
                throw new ResultException(params.getName() + "已存在");
            }
        }
        WorkshopEntity params1 = BeanUtil.copyProperties(params, WorkshopEntity.class);
        workshopService.save(params1);
        return Result.ok(HttpStatus.OK);
    }

    @ApiOperation("删除")
    @GetMapping(value = "/delete/{id}")
    @UserLog(desc = "内部倒运-删除车间", roleCode = "nbdy.workshop.del")
    public Result<Object> delete(@PathVariable("id") String id) {
        int count = goodsLocationService.count(new LambdaQueryWrapper<GoodsLocation>().eq(GoodsLocation::getWorkshopId, id));
        if (count > 0) return Result.err("该车间在装卸货点管理被使用了!");
        int count1 = plannedOrdersService.count(new LambdaQueryWrapper<PlannedOrdersEntity>().eq(PlannedOrdersEntity::getLocationUpId, id));
        if (count1 > 0) return Result.err("该车间在计划单管理被使用了!");
        int count2 = plannedOrdersService.count(new LambdaQueryWrapper<PlannedOrdersEntity>().eq(PlannedOrdersEntity::getLocationDownId, id));
        if (count2 > 0) return Result.err("该车间在计划单管理被使用了!");
        WorkshopEntity params = new WorkshopEntity();
        params.setId(Integer.valueOf(id));
        params.setDelFlag(DelFlagEnum.YES.getType());
        workshopService.updateById(params);
        return Result.ok(HttpStatus.OK);
    }

    @ApiOperation("修改")
    @PostMapping(value = "/update")
    @UserLog(desc = "内部倒运-修改车间", roleCode = "nbdy.workshop.update")
    public Result<Object> delete(@RequestBody WorkshopAddParam params) throws ResultException {
        if (StrUtil.isNotBlank(params.getName())) {
            LambdaQueryWrapper<WorkshopEntity> query1 = new LambdaQueryWrapper<>();
            query1.eq(WorkshopEntity::getName, params.getName());
            query1.eq(WorkshopEntity::getDelFlag, DelFlagEnum.NO.getType());
            query1.ne(WorkshopEntity::getId, params.getId());
            List<WorkshopEntity> list = workshopService.list(query1);
            if (CollUtil.isNotEmpty(list)) {
                throw new ResultException(params.getName() + "已存在");
            }
        }
        WorkshopEntity params1 = BeanUtil.copyProperties(params, WorkshopEntity.class);
        workshopService.updateById(params1);
        return Result.ok(HttpStatus.OK);
    }


    @ApiOperation("启用/禁用")
    @GetMapping(value = "/activate")
    @UserLog(desc = "内部倒运-修改车间", roleCode = "nbdy.workshop.update")
    public Result<Object> delete(@RequestParam("id") String id, @RequestParam("activateFlag") int activateFlag) {
        WorkshopEntity params = new WorkshopEntity();
        params.setId(Integer.valueOf(id));
        params.setActivateFlag(activateFlag);
        workshopService.updateById(params);
        return Result.ok(HttpStatus.OK);
    }


}
